from fastapi import APIRouter, Depends, Path
from typing import Annotated
import requests
from sqlalchemy.orm import Session
from sqlalchemy.exc import IntegrityError
from common.deps import get_db
from core.response import response_code
from exceptions import custom_exc
from core.schemas.classroom import ClassroomCreate, ClassroomUpdate
from datetime import datetime
import crud

router = APIRouter()

@router.post("classroom/create", summary="添加新的教室")
async def create_classroom(new_classroom:ClassroomCreate, db: Session = Depends(get_db)):
    res = crud.classroom.create(db,new_classroom)
    return response_code.resp_200(data=res)

@router.put("/classsroom/{id}", summary="修改教室信息")
async def modify_classroom(id: Annotated[int, Path(title="教室id编号")], db: Session = Depends(get_db)):
  #  res = crud.classroom.modify_classroom(db,update_classroom)
    return response_code.resp_200(data={})

@router.delete("/classroom/{id}", summary="删除教室")
async def delete_classroom(id: Annotated[int, Path(title="教室id编号")], db: Session = Depends(get_db)):
    res = crud.classroom.get_classroom_by_id(db,id)
    return response_code.resp_200(data=res)

@router.get("/user/list", summary="列出用户")
async def list_user(page: int = 1, pageSize: int = 20, db: Session = Depends(get_db)):
    res = crud.user.get_users(db,page,pageSize)
    return response_code.resp_200(data=res)


@router.put("/user/{id}/password", summary="修改密码")
async def change_user_password(id: Annotated[int, Path(title="用户id编号")], db: Session = Depends(get_db)):
    res = crud.classroom.get_classroom_by_id(db,id)
    return response_code.resp_200(data=res)

@router.delete("/user/{id}", summary="删除用户")
async def delete_user(id: Annotated[int, Path(title="用户id编号")], db: Session = Depends(get_db)):
    res = crud.classroom.get_classroom_by_id(db,id)
    return response_code.resp_200(data=res)

@router.get("/device/list", summary="列出设备")
async def list_device(page: int = 1, pageSize: int = 20, db: Session = Depends(get_db)):
    res = crud.device.list_all_devices(db,page,pageSize)
    return response_code.resp_200(data=res)

@router.post("/device/regisiter", summary="注册设备")
async def regisiter_device(id: Annotated[int, Path(title="用户id编号")], db: Session = Depends(get_db)):
    res = crud.classroom.get_classroom_by_id(db,id)
    return response_code.resp_200(data=res)

@router.put("/device/{id}", summary="更新设备")
async def update_device(id: Annotated[int, Path(title="用户id编号")], db: Session = Depends(get_db)):
    res = crud.classroom.get_classroom_by_id(db,id)
    return response_code.resp_200(data=res)

@router.delete("/device/{id}", summary="删除设备")
async def delete_device(id: Annotated[int, Path(title="用户id编号")], db: Session = Depends(get_db)):
    res = crud.classroom.get_classroom_by_id(db,id)
    return response_code.resp_200(data=res)

@router.get("/participant/checkin/{id}", summary="签到")
async def checkin_participant(id: Annotated[int, Path(title="用户id编号")], db: Session = Depends(get_db)):
    crud.participant.checkin(db,id,datetime.now())
    return response_code.resp_200(data={})

@router.get("/participant/checkout/{id}", summary="签到")
async def checkout_participant(id: Annotated[int, Path(title="用户id编号")], db: Session = Depends(get_db)):
    crud.participant.checkout(db,id,datetime.now())
    return response_code.resp_200(data={})

@router.get("/campusNews", summary="新闻")
async def campus_news(db: Session = Depends(get_db)):
    url = 'http://xsy.jstu.edu.cn/_wp3services/generalQuery?queryObj=articles'
    data = {
        'siteId': '43',
        'columnId': '7796,7797',
        'pageIndex': '1',
        'rows': '10',
        'orders': '[{"field":"visitCount","type":"desc"}]',
        'returnInfos': '[{"field":"title","pattern":[{"name":"lp","value":"30"}],"name":"title"},{"field":"publishTime","pattern":[{"name":"d","value":"yyyy-MM-dd"}],"name":"publishTime"}]',
        'conditions': '[{"field":"scope","value":1,"judge":"="},{"field":"publishTime","value":"2025-01-23","judge":">="}]'
    }
    response = requests.post(url, data=data, verify=False).json()
    
    return response_code.resp_200(data=response['data'])
